<template>
  <div>
    <el-dialog title="修改用户角色" @open="changeRoleOpen()" :visible.sync="dialogVisible" width="800px">
      <p>当前的用户 : {{ userInfo.username }}</p>
      <p>当前的角色 : {{ userInfo.role_name }}</p>
        <div>
          <span>分配新角色：</span>
          <el-select v-model="selectRoleId" placeholder="分配新角色">
            <el-option v-for="(item) in roleList" :key="item.id" :label="item.roleName" :value="item.id"></el-option>
          </el-select>
        </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeRoleInfo(userInfo)"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUserRoleInfoApi, changeRoleInfoApi } from '@/Utils/Api/getUserInfoApi'
export default {
  name: 'changeUserRole',
  data () {
    return {
      dialogVisible: false,
      userInfo: {
        username: '',
        role_name: ''
      },
      roleList: {},
      selectRoleId: ''
    }
  },
  components: {},
  created () {},
  mounted () {},
  methods: {
    // 获取全部角色
    async changeRoleOpen () {
      const { data } = await getUserRoleInfoApi()
      if (data.meta.status !== 200) {
        return this.$message({
          type: 'error',
          message: data.meta.msg
        })
      }
      this.roleList = data.data
      this.$message({
        type: 'success',
        message: data.meta.msg
      })
    },
    // 修改用户角色
    async changeRoleInfo () {
      const { data } = await changeRoleInfoApi(this.userInfo.id, { rid: this.selectRoleId })
      if (data.meta.status !== 200) {
        return this.$message({
          type: 'error',
          message: data.meta.msg
        })
      }
      this.$message({
        type: 'scuccess',
        message: data.meta.msg
      })
      this.$emit('changeRoleInfoDrive')
      this.dialogVisible = false
    }
  },
  computed: {},
  watch: {}
}
</script>

<style scoped>
</style>
